/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.unwiredgrain.modeling.sscm.impl;

import com.unwiredgrain.modeling.sscm.Port;
import com.unwiredgrain.modeling.sscm.Protocol;
import com.unwiredgrain.modeling.sscm.ProtocolTemplateInstantiation;

import com.unwiredgrain.modeling.sscm.PortDirection;
import com.unwiredgrain.modeling.sscm.SSCMPackage;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Facet</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#getProtocol <em>Protocol</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#getUid <em>Uid</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#getDirection <em>Direction</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#isMultiple <em>Multiple</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#getProtocolTemplateInstantiation <em>Protocol Template Instantiation</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#isOptional <em>Optional</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.PortImpl#getProtocolName <em>Protocol Name</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class PortImpl extends NamedElementImpl implements Port {
	/**
	 * The default value of the '{@link #getUid() <em>Uid</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getUid()
	 * @generated
	 * @ordered
	 */
	protected static final String UID_EDEFAULT = "default-guid";

	/**
	 * The cached value of the '{@link #getUid() <em>Uid</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getUid()
	 * @generated
	 * @ordered
	 */
	protected String uid = UID_EDEFAULT;

	/**
	 * The default value of the '{@link #getDirection() <em>Direction</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDirection()
	 * @generated
	 * @ordered
	 */
	protected static final PortDirection DIRECTION_EDEFAULT = PortDirection.PROVIDES;

	/**
	 * The cached value of the '{@link #getDirection() <em>Direction</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDirection()
	 * @generated
	 * @ordered
	 */
	protected PortDirection direction = DIRECTION_EDEFAULT;

	/**
	 * The default value of the '{@link #isMultiple() <em>Multiple</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isMultiple()
	 * @generated
	 * @ordered
	 */
	protected static final boolean MULTIPLE_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isMultiple() <em>Multiple</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isMultiple()
	 * @generated
	 * @ordered
	 */
	protected boolean multiple = MULTIPLE_EDEFAULT;

	/**
	 * The cached value of the '{@link #getProtocolTemplateInstantiation() <em>Protocol Template Instantiation</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProtocolTemplateInstantiation()
	 * @generated
	 * @ordered
	 */
	protected ProtocolTemplateInstantiation protocolTemplateInstantiation;

	/**
	 * The default value of the '{@link #isOptional() <em>Optional</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isOptional()
	 * @generated
	 * @ordered
	 */
	protected static final boolean OPTIONAL_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isOptional() <em>Optional</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isOptional()
	 * @generated
	 * @ordered
	 */
	protected boolean optional = OPTIONAL_EDEFAULT;

	/**
	 * The default value of the '{@link #getProtocolName() <em>Protocol Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProtocolName()
	 * @generated
	 * @ordered
	 */
	protected static final String PROTOCOL_NAME_EDEFAULT = null;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected PortImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SSCMPackage.Literals.PORT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Protocol getProtocol() {
		Protocol protocol = basicGetProtocol();
		return protocol != null && protocol.eIsProxy() ? (Protocol)eResolveProxy((InternalEObject)protocol) : protocol;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Protocol basicGetProtocol() {
		// DONE: implement this method to return the 'Protocol' reference
		// -> do not perform proxy resolution
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		if (getProtocolTemplateInstantiation() == null) {
			return null;
		}
		
		return getProtocolTemplateInstantiation().getProtocol();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setProtocol(Protocol newProtocol) {
		// DONE: implement this method to set the 'Protocol' reference
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		Protocol oldProtocol = getProtocol();
		
		// both null
		if (newProtocol == null && oldProtocol == null) {
			return;
		}
		
		// new null, old not null, remove unnecessary object.
		if (newProtocol == null) {
			setProtocolTemplateInstantiation(null);
			
			if (eNotificationRequired()) {
				eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__PROTOCOL, oldProtocol, newProtocol));
			}
			return;
		}
		
		// new not null, old whatever 
		if (getProtocolTemplateInstantiation() == null) {
			ProtocolTemplateInstantiation pti = SSCMFactoryImpl.eINSTANCE.createProtocolTemplateInstantiation();
			setProtocolTemplateInstantiation(pti);
		}
		
		getProtocolTemplateInstantiation().setProtocol(newProtocol);
		
		if (eNotificationRequired()) {
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__PROTOCOL, oldProtocol, newProtocol));
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PortDirection getDirection() {
		return direction;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDirection(PortDirection newDirection) {
		PortDirection oldDirection = direction;
		direction = newDirection == null ? DIRECTION_EDEFAULT : newDirection;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__DIRECTION, oldDirection, direction));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isMultiple() {
		return multiple;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setMultiple(boolean newMultiple) {
		boolean oldMultiple = multiple;
		multiple = newMultiple;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__MULTIPLE, oldMultiple, multiple));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ProtocolTemplateInstantiation getProtocolTemplateInstantiation() {
		return protocolTemplateInstantiation;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetProtocolTemplateInstantiation(ProtocolTemplateInstantiation newProtocolTemplateInstantiation, NotificationChain msgs) {
		ProtocolTemplateInstantiation oldProtocolTemplateInstantiation = protocolTemplateInstantiation;
		protocolTemplateInstantiation = newProtocolTemplateInstantiation;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION, oldProtocolTemplateInstantiation, newProtocolTemplateInstantiation);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setProtocolTemplateInstantiation(ProtocolTemplateInstantiation newProtocolTemplateInstantiation) {
		if (newProtocolTemplateInstantiation != protocolTemplateInstantiation) {
			NotificationChain msgs = null;
			if (protocolTemplateInstantiation != null)
				msgs = ((InternalEObject)protocolTemplateInstantiation).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION, null, msgs);
			if (newProtocolTemplateInstantiation != null)
				msgs = ((InternalEObject)newProtocolTemplateInstantiation).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION, null, msgs);
			msgs = basicSetProtocolTemplateInstantiation(newProtocolTemplateInstantiation, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION, newProtocolTemplateInstantiation, newProtocolTemplateInstantiation));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isOptional() {
		return optional;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setOptional(boolean newOptional) {
		boolean oldOptional = optional;
		optional = newOptional;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__OPTIONAL, oldOptional, optional));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String getProtocolName() {
		// DONE: implement this method to return the 'Protocol Name' attribute
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		if (getProtocolTemplateInstantiation() == null) {
			return null;
		}
		return getProtocolTemplateInstantiation().getCppName();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String getUid() {
		if (uid == null || uid == PortImpl.UID_EDEFAULT) {
			uid = java.util.UUID.randomUUID().toString();
		}
		return uid;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setUid(String newUid) {
		String oldUid = uid;
		uid = newUid;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.PORT__UID, oldUid, uid));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION:
				return basicSetProtocolTemplateInstantiation(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SSCMPackage.PORT__PROTOCOL:
				if (resolve) return getProtocol();
				return basicGetProtocol();
			case SSCMPackage.PORT__UID:
				return getUid();
			case SSCMPackage.PORT__DIRECTION:
				return getDirection();
			case SSCMPackage.PORT__MULTIPLE:
				return isMultiple();
			case SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION:
				return getProtocolTemplateInstantiation();
			case SSCMPackage.PORT__OPTIONAL:
				return isOptional();
			case SSCMPackage.PORT__PROTOCOL_NAME:
				return getProtocolName();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SSCMPackage.PORT__PROTOCOL:
				setProtocol((Protocol)newValue);
				return;
			case SSCMPackage.PORT__UID:
				setUid((String)newValue);
				return;
			case SSCMPackage.PORT__DIRECTION:
				setDirection((PortDirection)newValue);
				return;
			case SSCMPackage.PORT__MULTIPLE:
				setMultiple((Boolean)newValue);
				return;
			case SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION:
				setProtocolTemplateInstantiation((ProtocolTemplateInstantiation)newValue);
				return;
			case SSCMPackage.PORT__OPTIONAL:
				setOptional((Boolean)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SSCMPackage.PORT__PROTOCOL:
				setProtocol((Protocol)null);
				return;
			case SSCMPackage.PORT__UID:
				setUid(UID_EDEFAULT);
				return;
			case SSCMPackage.PORT__DIRECTION:
				setDirection(DIRECTION_EDEFAULT);
				return;
			case SSCMPackage.PORT__MULTIPLE:
				setMultiple(MULTIPLE_EDEFAULT);
				return;
			case SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION:
				setProtocolTemplateInstantiation((ProtocolTemplateInstantiation)null);
				return;
			case SSCMPackage.PORT__OPTIONAL:
				setOptional(OPTIONAL_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SSCMPackage.PORT__PROTOCOL:
				return basicGetProtocol() != null;
			case SSCMPackage.PORT__UID:
				return UID_EDEFAULT == null ? uid != null : !UID_EDEFAULT.equals(uid);
			case SSCMPackage.PORT__DIRECTION:
				return direction != DIRECTION_EDEFAULT;
			case SSCMPackage.PORT__MULTIPLE:
				return multiple != MULTIPLE_EDEFAULT;
			case SSCMPackage.PORT__PROTOCOL_TEMPLATE_INSTANTIATION:
				return protocolTemplateInstantiation != null;
			case SSCMPackage.PORT__OPTIONAL:
				return optional != OPTIONAL_EDEFAULT;
			case SSCMPackage.PORT__PROTOCOL_NAME:
				return PROTOCOL_NAME_EDEFAULT == null ? getProtocolName() != null : !PROTOCOL_NAME_EDEFAULT.equals(getProtocolName());
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (uid: ");
		result.append(uid);
		result.append(", direction: ");
		result.append(direction);
		result.append(", multiple: ");
		result.append(multiple);
		result.append(", optional: ");
		result.append(optional);
		result.append(')');
		return result.toString();
	}

} //FacetImpl
